#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time

import utils
from config import LOGIN_URL, LOGIN_USERNAME, LOGIN_PASSWORD, CHANNEL_URL


def check_browser(func):
    def wrapper(self, *args, **kwargs):
        if not self._browser:
            print("Failed to get _browser")
        else:
            func(self, *args, **kwargs)

    return wrapper


class Login(object):
    def __init__(self):
        self._browser = utils.get_browser()
        print("login browser %x" % id(self._browser))
        self._browser.get(LOGIN_URL)

    @check_browser
    def login(self):
        self._browser.get(LOGIN_URL)
        time.sleep(2)

        # 登录：填写用户名
        username = self._browser.find_element_by_id("username")
        username.send_keys(LOGIN_USERNAME)

        # 登录：填写密码
        password = self._browser.find_element_by_id("password")
        password.send_keys(LOGIN_PASSWORD)

        # 点击登录按钮
        submit = self._browser.find_element_by_name("submit")
        submit.click()

        # 点击新建频道
        self._browser.get(CHANNEL_URL)
